package ModelLayer;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;

public class GroupRecords {
	
	private ArrayList<Group> groups;
	private static GroupRecords instance=null;
	
	private GroupRecords()
	{
		groups= new ArrayList<Group>();	
	}
	
	public static GroupRecords getInstance()
	{
		if(instance==null)
		{
			instance=new GroupRecords();
		}
		return instance;
	}
	
	public void createGroup(int paymentTerm, double craftmanDiscount, double quantityDiscount, double pickUpDiscount) throws RealException 
	{
		Group newGroup=new Group(paymentTerm,craftmanDiscount,quantityDiscount,pickUpDiscount);
		groups.add(newGroup);
	}
	
	public Group getGroup(int id) throws RealException
	{
		boolean found = false;
		Group theGroup = null;
		if(groups.size()!=0)
		{
			Iterator<Group> it=groups.iterator();
			while(it.hasNext() && !found)
			{
				Group group=it.next();
				if(group.getId()==id)
				{
					theGroup = group;
					found = true;
				}
			}
		}
		if(!found) throw new RealException("Group not found!");
		return theGroup;
	}
	
	public void checkGroup(int id) throws RealException
	{
		boolean found = false;
		if(groups.size()!=0)
		{
			Iterator<Group> it=groups.iterator();
			while(it.hasNext() && !found)
			{
				Group group=it.next();
				if(group.getId()==id)
				{
					found = true;
				}
			}
		}
		if(!found) throw new RealException("Group not found!");
	}
	
	public void updateGroup(int id ,int choise, String newValue) throws RealException
	{
		Group group=getGroup(id);
		if(group!=null)
		{
			switch(choise)
			{
				case 1:
				{
					group.setPaymentTerm(Integer.parseInt(newValue));
					break;
				}
				case 2:
				{
					group.setCraftmanDiscount(Double.parseDouble(newValue));
					break;
				}
				case 3:
				{
					group.setQuantityDiscount(Double.parseDouble(newValue));
					break;
				}
				case 4:
				{
					group.setPickUpDiscount(Double.parseDouble(newValue));
					break;
				}
			}	
		}
	}

	public void deleteGroup(int id) throws RealException
	{
		Group group=getGroup(id);
		if(group!=null)
			groups.remove(group);
	}

	public String printGroup(int id) throws RealException
    {
    	return getGroup(id).toString();
    }
	
	@SuppressWarnings("unchecked")
	public void load(ObjectInputStream obj_in) throws IOException, ClassNotFoundException
	{
		this.groups = (ArrayList<Group>) obj_in.readObject();	
		Group.setLastId(((Integer)obj_in.readObject()).intValue());
	}
	
	public void save(ObjectOutputStream obj_out) throws IOException
	{		
		obj_out.writeObject(groups);
		obj_out.writeObject(new Integer(Group.getLastId()));
	}
	
	public ArrayList<String> getGrops()
	{
		ArrayList<String> list=new ArrayList<String>();
		if(groups.size()!=0)
		{
			for(Group group: groups)
			{
				list.add(group.toString());
			}
		}
		return list;
	}
	
	public ArrayList<Group> getGroupsAL()
	{
		ArrayList<Group> list = new ArrayList<Group>();
		if(groups.size()!=0)
		{
			for (Group group: groups)
			{
				list.add(group);
			}
		}
		return list;
	}
}
